package org.rendao.service
{
	import com.googlecode.flexxb.FlexXBEngine;
	
	import org.rendao.entity.SearchResult;
	import org.rendao.entity.UserDetail;
	import org.rendao.service.base.ServiceUtils;

	public class UserService
	{
		private static var _instance:UserService;
		
		public static function get instance():UserService {
			if (!_instance) {
				_instance=new UserService(new SingletonForcer());
			}
			return _instance;
		}
		
		public function UserService(sf:SingletonForcer) {
		}
		
		private static const GET_ALL_URL:String = "service/user/get_all";
		public function getAllUser(callback:Function):void {
			var onGet:Function = function(result:Object):void {
				var resultXml:XML = result as XML;
				var searchResult:SearchResult = FlexXBEngine.instance.deserialize(resultXml, SearchResult);
				callback(searchResult.records);
			};
			ServiceUtils.callService(GET_ALL_URL, onGet);
		}
		
		private static const SAVE_USER_URL:String = "service/user/save";
		public function saveUser(user:UserDetail, callback:Function):void {
			var onSave:Function = function(result:Object):void {
				var resultXml:XML = result as XML;
				var savedUser:UserDetail = FlexXBEngine.instance.deserialize(resultXml, UserDetail);
				callback(savedUser);
			};
			var content:XML = FlexXBEngine.instance.serialize(user);
			ServiceUtils.callService(SAVE_USER_URL, onSave, content);
		}
		
		private static const GET_ALL_ROLES:String = "service/role/get_all";
		public function getAllRoles(callback:Function):void {
			var onGet:Function = function(result:Object):void {
				var resultXml:XML = result as XML;
				var searchResult:SearchResult = FlexXBEngine.instance.deserialize(resultXml, SearchResult);
				callback(searchResult.records);
			};
			ServiceUtils.callService(GET_ALL_ROLES, onGet);
		}
		
		private static const DELETE_USER_URL:String = "service/user/delete/{id}";
		public function deleteUser(id:String, callback:Function):void {
			var onDelete:Function = function(result:Object):void {
				callback();
			};
			var url:String = DELETE_USER_URL.replace("{id}", id);
			ServiceUtils.callService(url, onDelete);
		}
	}
}
class SingletonForcer
{
}